﻿using System;
using System.Runtime.InteropServices;

namespace RA2VXLEncryption
{
    // 参考资料：
    // [1] https://www.cnblogs.com/cmtspace/p/14356272.html

    public class VPL
    {
        static VPL()
        {
            string dllFile = Environment.Is64BitProcess ?
                "./DLL/x64/vpl.dll" :
                "./DLL/x86/vpl.dll";
            LoadLibraryA(dllFile);
        }

        [DllImport("kernel32")]
        private static extern IntPtr LoadLibraryA([MarshalAs(UnmanagedType.LPStr)] string fileName);

        /// <summary>
        /// 加密指定的VPL文件并生成对应的密钥
        /// </summary>
        /// <param name="vplSource">未加密的VPL路径</param>
        /// <param name="vplDest">生成加密后的VPL路径</param>
        /// <param name="keyBuffer">密钥</param>
        /// <param name="encryptScriptPath">生成的对应cscheme加密脚本[可NULL]</param>
        /// <returns>操作是否成功</returns>
        [DllImport("vpl.dll", EntryPoint = "Encrypt_VPL_File")]
        public static extern bool EncryptVPL(
            IntPtr vplSource,
            IntPtr vplDest,
            IntPtr keyBuffer,
            IntPtr encryptScriptPath);

        /// <summary>
        /// 根据指定的密钥还原VPL文件
        /// </summary>
        /// <param name="vplSource">加密过的VPL文件路径</param>
        /// <param name="vplDest">还原的VPL文件路径</param>
        /// <param name="keyBuffer">密钥</param>
        /// <returns>操作是否成功</returns>
        [DllImport("vpl.dll", EntryPoint = "Decrypt_VPL_File")]
        public static extern bool DecryptVPL(
            IntPtr vplSource,
            IntPtr vplDest,
            IntPtr keyBuffer);

        /// <summary>
        /// 将密钥逆向或导出对应的cscheme解密脚本
        /// </summary>
        /// <param name="keyBuffer">密钥</param>
        /// <param name="decryptScriptPath">生成的对应cscheme解密脚本[可NULL]</param>
        /// <param name="keyDest">逆向的密钥[可NULL]</param>
        /// <returns>操作是否成功</returns>
        [Obsolete]
        [DllImport("vpl.dll", EntryPoint = "Decrypt_Key")]
        public static extern bool DecryptKey(
            IntPtr keyBuffer,
            IntPtr decryptScriptPath,
            IntPtr keyDest);

        /// <summary>
        /// 用指定的密钥加密VXL文件
        /// </summary>
        /// <param name="keyBuffer">密钥</param>
        /// <param name="vxlSource">未加密的VXL文件路径</param>
        /// <param name="vxlDest">加密后的VXL文件路径</param>
        /// <returns>操作是否成功</returns>
        [DllImport("vpl.dll", EntryPoint = "Encrypt_VXL")]
        public static extern bool EncryptVXL(
            IntPtr keyBuffer,
            IntPtr vxlSource,
            IntPtr vxlDest);

        /// <summary>
        /// 用指定的密钥解密VXL文件
        /// </summary>
        /// <param name="keyBuffer">密钥</param>
        /// <param name="vxlSource">加密后的VXL文件路径</param>
        /// <param name="vxlDest">解密后的VXL文件路径</param>
        /// <returns>操作是否成功</returns>
        [DllImport("vpl.dll", EntryPoint = "Decrypt_VXL")]
        public static extern bool DecryptVXL(
            IntPtr keyBuffer,
            IntPtr vxlSource,
            IntPtr vxlDest);
    }
}
